import { createRouter, createWebHashHistory } from "vue-router"

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            name: 'home',
            component: () => import('@/views/home')
        },
        {
            path: '/home',
            name: 'home',
            component: () => import('@/views/home')
        },
        {
            path: '/cart',
            name: 'cart',
            component: () => import('@/views/cart')
        },
        {
            path: '/mine',
            component: () => import('@/views/mine')
        },
        {
            path: '/order',
            name: 'order',
            component: () => import('@/views/order')
        },
        {
            path: '/search',
            name: 'search',
            component: () => import('@/views/search'),
            meta: { hideTabbar: true }
        },
        {
            path: '/city',
            name: 'city',
            component: () => import('@/views/city'),
            meta: { hideTabbar: true }
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login'),
            meta: { hideTabbar: true }
        },
        {
            path: '/phoneNumberPlace',
            name: 'phoneNumberPlace',
            component: () => import('@/views/phoneNumberPlace'),
            meta: { hideTabbar: true }
        },
        {
            path: '/account',
            name: 'account',
            component: () => import('@/views/account'),

        },
        {
            path: '/detail/:goodsId',
            name: 'detail',
            component: () => import('@/views/detail'),
            meta: { hideTabbar: true }
        },

    ]
})

const list = ['search']
router.beforeEach(async (to, from) => {
    // console.log(from,to)
    if (
        !localStorage.getItem('userId') &&
        list.find(item => item === to.name)
    ) {
        // 将用户重定向到登录页面
        return { name: 'login' }
    }
})

export default router